package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.SDCardImageLoader;

/**
 * PhotoWall中GridView的适配器
 */

public class PhotoWallAdapter extends BaseAdapter {
    //上下文
    private Context context;
    //选择图片本地路径
    private ArrayList<String> imagePathList = null;
    //SDK图片加载器
    private SDCardImageLoader loader;
    //按顺序记录选中状态
    private List<String> selectionList;
    //最多选择图片个数，默认是1
    private int maxSelectCount = 1;
    //选择图片监听
    public OnSelectImageListener mOnSelectImageListener;

    public PhotoWallAdapter(Context context, ArrayList<String> imagePathList, List<String> selectedList, int maxSelectCount, OnSelectImageListener listener) {
        this.context = context;
        this.imagePathList = imagePathList;
        loader = HttpUtils.getSDCardImageLoader();
        selectionList = new ArrayList<>();
        if (selectedList != null) {
            for (String path : selectedList) {
                selectionList.add(path);
            }
        }
        this.maxSelectCount = maxSelectCount;
        mOnSelectImageListener = listener;
    }

    @Override
    public int getCount() {
        return imagePathList == null ? 0 : imagePathList.size();
    }

    @Override
    public Object getItem(int position) {
        return imagePathList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String filePath = (String) getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.photo_wall_item, null);
            holder = new ViewHolder(convertView, this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.bindViewHolder(filePath);
        //是否选中
        int imageIndex = selectionList.indexOf(filePath);
        if (imageIndex > -1) {
            holder.setSelected(imageIndex);
        } else {
            holder.setUnSelected();
        }
        loader.loadImage(4, filePath, holder.imageView);
        return convertView;
    }

    private static class ViewHolder implements View.OnClickListener {
        //图片
        private ImageView imageView;
        //选择图片
        private TextView indexTextView;
        //选择的图层
        private View selectLayer;
        //选择图片路径
        private String mImagePath;
        private PhotoWallAdapter mPhotoWallAdapter;

        public ViewHolder(View view, PhotoWallAdapter photoWallAdapter) {
            mPhotoWallAdapter = photoWallAdapter;
            imageView = (ImageView) view.findViewById(R.id.photo_wall_item_photo);
            selectLayer = view.findViewById(R.id.select_image_layer);
            indexTextView = (TextView) view.findViewById(R.id.photo_wall_item_index);
            view.setOnClickListener(this);
        }

        public void bindViewHolder(String imagePath) {
            mImagePath = imagePath;
        }

        @Override
        public void onClick(View view) {
            if (isSelect()) {//应该取消
                mPhotoWallAdapter.selectionList.remove(mImagePath);
                mPhotoWallAdapter.mOnSelectImageListener.cancelSelectImage(mImagePath, mPhotoWallAdapter.selectionList.size());
            } else {//应该选中
                int size = mPhotoWallAdapter.selectionList.size();
                Boolean isPass = mPhotoWallAdapter.mOnSelectImageListener.beforeSelectImage(mImagePath, size);
                if (!isPass) {
                    return;
                }
                mPhotoWallAdapter.selectionList.add(mImagePath);
                mPhotoWallAdapter.mOnSelectImageListener.onSelectImage(mImagePath, size + 1);
            }
            mPhotoWallAdapter.notifyDataSetChanged();
        }

        /**
         * 判断是否选中状态
         *
         * @return
         */
        public Boolean isSelect() {
            boolean is = mPhotoWallAdapter.selectionList.indexOf(mImagePath) > -1;
            return is;
        }

        /**
         * 设置成选中状态
         *
         * @param index
         */
        public void setSelected(int index) {
            indexTextView.setText("" + (index + 1));
            indexTextView.setVisibility(View.VISIBLE);
            selectLayer.setVisibility(View.VISIBLE);
        }

        /**
         * 设置成未选中状态
         */
        public void setUnSelected() {
            indexTextView.setVisibility(View.GONE);
            selectLayer.setVisibility(View.GONE);
        }
    }

    /**
     * 获取选中的图片
     *
     * @return
     */
    public List<String> getSelectionList() {
        return selectionList;
    }

    /**
     * 选中某个图片
     *
     * @param filePath
     */
    public void selectItem(String filePath) {
        mOnSelectImageListener.beforeSelectImage(filePath, selectionList.size());
        if (selectionList.size() >= maxSelectCount) {
            notifyDataSetChanged();
            return;
        }
        int size = selectionList.size();
        selectionList.add(filePath);
        mOnSelectImageListener.onSelectImage(filePath, size + 1);
        notifyDataSetChanged();
    }


    /**
     * 选中事件处理
     */
    public interface OnSelectImageListener {
        /**
         * 选中之前调用
         *
         * @param filePath
         * @param selectCount
         */
        Boolean beforeSelectImage(String filePath, int selectCount);

        /**
         * 选中之后调用
         *
         * @param filePath
         * @param selectCount
         */
        void onSelectImage(String filePath, int selectCount);

        /**
         * 取消选中调用
         *
         * @param filePath
         * @param selectCount
         */
        void cancelSelectImage(String filePath, int selectCount);
    }
}
